﻿using System;
using Renci.SshNet;
using System.IO;
using System.Threading;
using System.Net.NetworkInformation;

namespace PenguinPublishNS
{
    public class sftpUtils
    {
        // SftpClient functions are implemented here. Build a ConnectionInfo object and test connectivity

        // Arrange tests in such a way as to be self cleaning. No residual files on remote host. Allow time between.

        public static bool checkFxs(ConnectionInfo curHostConn, string _remotePath)
        {
            // setup variables
            string fName = PenguinPublish.dataPath + @"\test.txt";  // TODO: make a file on the fly
            string shortName = Path.GetFileName(fName);
            string remoteFile = _remotePath + "/" + shortName;
            string oldPath = _remotePath + @"/newDir";
            string newPath = _remotePath + @"/pruneDir";

            // TODO: if this function fails part way through, there is no cleanup of the remains
            try
            {
                SftpClient sftp = new SftpClient(curHostConn);
                sftp.ConnectionInfo.Timeout = TimeSpan.FromSeconds(2);  // this works and is global for all operations
                sftp.Connect();
                sftp.CreateDirectory(oldPath);
                Thread.Sleep(100);
                using (var file = File.Create(fName))
                {
                    sftp.UploadFile(file, remoteFile, true);
                }
                Thread.Sleep(100);
                sftp.DeleteFile(remoteFile);
                Thread.Sleep(100);
                sftp.RenameFile(oldPath, newPath);
                Thread.Sleep(100);
                sftp.DeleteDirectory(newPath);
            }
            catch (Renci.SshNet.Common.SshOperationTimeoutException)
            {
                System.Windows.MessageBox.Show("sftp timeout");
                throw;
            }

            return true;
        }


        public static bool chkHostIp(string _hostAddress)
        {
            if (string.IsNullOrEmpty(_hostAddress))return false;

            PingReply pingReply;
            using (var ping = new Ping())
                pingReply = ping.Send(_hostAddress);
            var available = pingReply.Status == IPStatus.Success;
            //if (available) System.Windows.MessageBox.Show(_hostAddress + ": the host responded");
            return available;
        }


        public static ConnectionInfo initializeSFTP(
            string hostAddress,
            string uName,
            bool usePrivateKey,
            string uPassword = "",
            string ppkPassPhrase = "",
            string ppkPath = ""
            )
        {
            // create ConnectionInfo object here, call on function below to produce the Authentication object
            AuthenticationMethod myAuth = createAuthObject(uName, usePrivateKey, uPassword, ppkPassPhrase, ppkPath);
            ConnectionInfo sftpConn = new ConnectionInfo(hostAddress, uName, myAuth);
            return sftpConn;
        }


        private static AuthenticationMethod createAuthObject(
            string _uName,
            bool _usePrivateKey,
            string _uPassword = "",
            string _ppkPassPhrase = "",
            string _ppkPath = ""
            )
        {
            PrivateKeyFile ppkFile;
            if (_usePrivateKey)
            {
                if (_ppkPassPhrase != "")
                {
                    ppkFile = new PrivateKeyFile(_ppkPath, _ppkPassPhrase);
                }
                else
                {
                    ppkFile = new PrivateKeyFile(_ppkPath);
                }
                PrivateKeyAuthenticationMethod myAuth = new PrivateKeyAuthenticationMethod(_uName, ppkFile);
                return myAuth;
            }
            else
            {
                PasswordAuthenticationMethod myAuth = new PasswordAuthenticationMethod(_uName, _uPassword);
                return myAuth;
            }
        }


    }
}
